﻿<template>

    <div v-loading="isLoading">

<!--        <h1>录入分类管理</h1>-->
      <page-part title="录入分类" style="padding-top: 10px">
      <query-table ref="record_query_table" :page-size="-1" :columns="tableColumns"
                   :default-query-params="{modelId:modelInfo.id}"
                   :detail-fields="tableInfoFields" base-url="/table"
                   :before-display-list="beforeDisplayFieldList"
                   show-index></query-table>
      </page-part>

<!--      <template v-if="currentInputTable.id&&currentInputTable.id.length>0">-->
<!--        <h1>录入指标管理 - {{currentInputTable.name}}</h1>-->
      <page-part v-if="currentInputTable.id&&currentInputTable.id.length>0" :title="'录入指标 - '+currentInputTable.name">
          <query-table ref="record_field_query_table" :page-size="-1" :columns="fieldColumns" :query-fields="[{id:'tableId',hide:true,defaultValue:currentInputTable.id}]" :detail-fields="fieldInfoFields" base-url="/field" show-index></query-table>
      </page-part>
<!--        </template>-->
    </div>

</template>

<script>
// import Sortable from 'sortablejs';
export default {
  name: "RecordModule",
  $$route: {
    path: 'record-module',
    parentName: 'ModelInfo',
  },
  props: {
    modelInfo: {},
  },
  data() {
    return {
      isFieldLoading: false,
      isLoading: false,
      formLabelWidth: '120px',
      inputFieldList: [],
      inputFieldDlgVisible: false,
      inputField: {computeUnit: {ref: ''}, dataType: 'number', defaultValue: ''},
      currentInputTable: {},
      modelEditable: false,
      defaultValueSourceMap: {},//默认值来源集合
      defaultValueMap: {},//默认值集合

      //录入分类展示列信息
      tableColumns: [
        {id: 'name', name: '名称', type: 'office', align: 'left'},
        {
          id: 'single', name: '年份相关', width: 100, showOverflowTooltip: true,
          formatter: (val) => {
            if (val) {
              return '不相关';
            }
            return '相关';
          }
        },
        {id: 'needLastYear', name: '上年数据',
          width: 80,
          formatter: (val) => {
            if (val) {
              return '需要';
            }
            return '不需要';
          }
        },
        {id: 'sort', name: '排序值', width: 80},
        {id: 'createTime', name: '创建时间', width: 150,},
        {id: 'createUser', name: '创建人', type: 'user', width: 80, showOverflowTooltip: true},
        {
          id: 'operation', name: '操作', type: 'button', width: 230,
          align: 'right',
          header: {
            type: 'button',
            children: [
              {text: '添加', permission:'model:update', operation: 'create', type: 'primary',
                isHide:(row) =>{
                  return !this.modelEditable;
                }
              }
            ]
          },
          children: [
            {text: '查看', operation: 'view',
              isHide:(row) =>{
                return this.modelEditable;
              }
            },
            {text: '编辑', operation: 'edit',permission:'model:update',
              isHide:(row)=>{
                return !this.modelEditable;
              }
            },
            {text: '指标列表', click: this.loadInputFieldList},
            {
              text: '删除', type: 'danger', operation: 'delete',
              isHide:(row)=>{
                return !this.modelEditable;
              }
            },
          ]
        },
      ],

      tableInfoFields: [{
        id: 'name',
        name: '分类名称',
        rules: [{required: true, message: "请输入名称", trigger: "blur"}],
        colSpan: 24,
      }, {
        id: 'single',
        name: '年份相关',
        component: {
          id: 'radio',
          optionList: [ {
            id: false,
            name: '相关'
          },{
            id: true,
            name: '不相关'
          }]
        },
        defaultValue: true,
        colSpan: 24
      }, {
        id: 'displayColumn',
        name: '显示列数',
        defaultValue: 3,
        isHide: (data) => {
          //年份相关时不展示
          if (data.single) {
            return false;
          }
          return true;
        },
        colSpan: 24

      }, {
        id: 'needLastYear',
        name: '上年数据',
        defaultValue: false,
        component: {
          id: 'radio',
          optionList: [{
            id: true,
            name: '需要'
          }, {
            id: false,
            name: '不需要'
          }],
        },
        isHide: (data) => {
          //年份相关时展示
          if (data.single) {
            return true;
          }
          return false;
        },
        colSpan: 24
      }, {
        id: 'showSum',
        name: '合计行',
        defaultValue: false,
        component: {
          id: 'radio',
          optionList: [{
            id: true,
            name: '需要'
          }, {
            id: false,
            name: '不需要'
          }],
        },
        isHide: (data) => {
          //年份相关时展示
          if (data.single) {
            return true;
          }
          return false;
        },
        colSpan: 24
      }, {
        id: 'icon',
        name: '展示图标',
        component: {
          id: 'icon-picker',
          type: 'product-type'
        },
        colSpan: 24
      }, {
        id: 'sort',
        name: '排序值',
        colSpan: 24
      }, {
        id: 'remark',
        name: '备注信息',
        component: {
          id: 'textarea',
          maxLength: 1000,
        },
        colSpan: 24
      }, {
        id: 'modelId', name: '模型id', hide: true,
        defaultValue: this.modelInfo.id
      }],

      //录入字段展示列信息
      fieldColumns: [
        {id: 'name', name: '指标名称', type: 'office', align: 'left'},
        {id: 'ref', name: '引用标识', width: 80, showOverflowTooltip: true},
        {id: 'dataType', name: '指标类型',type:'meta',source:'data-type', width: 80,},
        {id: 'unit', name: '指标单位',type:'meta',source:'unit-type', width: 80,},
        {id: 'defaultValueSource', name: '默认值来源',type:'meta',source:'init-data-source', width: 100,},
        {id: 'defaultValue', name: '默认值', width: 100, showOverflowTooltip: true},
        {id: 'show', name: '是否显示', width: 80, showOverflowTooltip: true,
          formatter:(val)=> {
            return val ? '是' : '否';
          }
        },
        {id: 'sort', name: '排序值', width: 80},
        {id: 'createTime', name: '创建时间', width: 150,},
        {id: 'createUser', name: '创建人', type: 'user', width: 80, showOverflowTooltip: true},
        {
          id: 'operation', name: '操作', type: 'button', width: 150,
          align: 'right',
          header: {
            type: 'button',
            children: [
              {text: '添加', permission:'model:update', operation:'create', type: 'primary',
                isHide:(row) =>{
                  return !this.modelEditable;
                }
              }
            ]
          },
          children: [
            {text: '查看', operation: 'view',
              isHide:(row) =>{
                return this.modelEditable;
              }
            },
            {text: '编辑', operation: 'edit',permission:'model:update',
              isHide:(row)=>{
                return !this.modelEditable;
              }
            },
            {
              text: '删除', type: 'danger', operation: 'delete',
              isHide:(row)=>{
                return !this.modelEditable;
              }
            },
          ]
        },
      ],

      fieldInfoFields: [{
        id: 'name',
        name: '指标名称',
        rules: [{required: true, message: "请输入名称", trigger: "blur"}],
        colSpan: 24,
      }, {
        id: 'dataType',
        name: '数据类型',
        component: {
          id:'meta-select',
          type: 'data-type',
          change:(value,fieldData)=>{
            let result = '';
            if ('boolean' == value) {
              result = 'false';
            }
            fieldData.defaultValue = result;
          }
        },
        defaultValue: 'number',
        colSpan: 24
      }, {
        id: 'unit',
        name: '数据单位',
        component: 'meta-select',
        type: 'unit-type',
        colSpan: 24
      }, {
        id: 'defaultValueSource',
        name: '默认值来源',
        component:{
          id:'meta-select',
          type:'init-data-source',
        },
        defaultValue: 'input',
        colSpan: 24,
      }, {
        id: 'defaultValue',
        name: '默认值',
        colSpan: 24,
        isHide:(data)=>{
          //如果默认值来源不是自定义输入或者是自定义输入且为布尔值，则隐藏
          return data.defaultValueSource!='input'||data.dataType=='boolean';
        }
      }, {
        id: 'defaultValue',
        name: '默认值',
        component: {
          id: 'radio',
          optionList: [{
            id: 'true',
            name: '是'
          }, {
            id: 'false',
            name: '否'
          }],
        },
        colSpan: 24,
        isHide:(data)=>{
          //如果默认值来源不是自定义输入或者是自定义输入且不为布尔值，则隐藏
          return data.defaultValueSource!='input'||data.dataType!='boolean';
        }
      }, {
        id: 'defaultValue',
        name: '默认值',
        parent:'defaultValueSource',
        component: {
          id: 'data-select',
          valueKey:'code',
          getDataUrl:(data) =>{
            //非自定义输入的默认值来源会提供dataUrl来进行数据加载
            if(this.defaultValueSourceMap&&this.defaultValueSourceMap[data.defaultValueSource]) {
              return this.defaultValueSourceMap[data.defaultValueSource].dataUrl;
            }
            return null;
          },
          load:(dataList,fieldData) => {
            this.loadDefaultValueList(dataList,fieldData.defaultValueSource);
          }
        },
        colSpan: 24,
        isHide:(data)=>{
          //如果默认值来源是自定义输入，则隐藏
          return data.defaultValueSource=='input';
        }
      }, {
        id: 'dataFillType',
        name: '年份数据填充',
        component: {
          id: 'meta-select',
          type:'year-data-fill-type',
        },
        defaultValue: 'all_year',
        colSpan: 24,
        isHide:(data)=>{
          //年份无关或者年份相关分类但默认值为年份相关时不需要展示
          return this.currentInputTable.single||!this.isDefaultValueSingle(data.defaultValueSource,data.defaultValue);
        }
      }, {
        id: 'showSum',
        name: '合计值',
        defaultValue: false,
        component: {
          id: 'radio',
          optionList: [{
            id: true,
            name: '需要'
          }, {
            id: false,
            name: '不需要'
          }],
        },
        colSpan: 24
      }, {
        id: 'show',
        name: '是否显示',
        defaultValue: true,
        component: {
          id: 'radio',
          optionList: [{
            id: true,
            name: '是'
          }, {
            id: false,
            name: '否'
          }],
        },
        colSpan: 24
      },
        {
        id: 'sort',
        name: '排序值',
        colSpan: 24
      }, {
        id: 'tableId', name: '分类id', hide: true,
        getDefaultValue:()=>{
          return this.currentInputTable.id;
        }
      }
      ],

    }
  },

  created: function () {
    // const modelId = this.$route.params.id;
    // this.loadModelInfo(modelId);
    this.modelEditable = this.modelInfo.state != 'publish';
    this.loadMetaMap('init-data-source',(map)=>{
      this.defaultValueSourceMap=map;
    })
  },
  methods: {
    getCurrentTable(){
      return this.currentInputTable.id;
    },
    // loadModelInfo: function (modelId) {
    //     this.$get("/model/" + modelId, {}, (response) => {
    //         this.modelInfo = response.data;
    //         this.modelEditable = this.modelInfo.state == 0;
    //     });
    // },
    loadInputFieldList: function (table) {
      // this.isFieldLoading = true;
      this.currentInputTable = table;
      this.$nextTick(()=>{
        if(this.$refs['record_field_query_table']) {
          this.$refs['record_field_query_table'].updateQueryCondition('tableId',this.currentInputTable.id,true);
        }
      })
      // this.$get("/field/data?tableId=" + table.id, {}, (response) => {
      //   let list = response.data;
      //   for (let item of list) {
      //     if (!item.defaultValueSource) {
      //       item.defaultValueSource = 'input';
      //     }
      //   }
      //   this.inputFieldList = list;
      //
      // }, () => {
      //   this.isFieldLoading = false;
      // });
    },


    loadDefaultValueList(dataList, dataSource) {
      let map = {};
      for (let item of dataList) {
        //注意此处用code而不是用id
        map[item.code] = item;
        //判断是否年份相关
        if (item.yearRelated) {
          item.name = item.name + '(年份相关)'
        }
      }
      this.defaultValueMap[dataSource] = map;
    },

    //默认值是否是单个值
    isDefaultValueSingle(dataSource,dataId) {
      if (!dataSource || dataSource == 'input') {
        return true;
      }
      if (!this.defaultValueMap[dataSource] || !this.defaultValueMap[dataSource][dataId]) {
        return true;
      }
      if (this.defaultValueMap[dataSource][dataId].yearRelated) {
        return false;
      }
      return true;
    },
    //处理字段请求完成后的回调事件
    beforeDisplayFieldList(dataList) {
      for (let item of dataList) {
        if (!item.defaultValueSource) {
          //默认值来源如果不存在则默认为自定义输入
          item.defaultValueSource = 'input';
        }
      }
    }
  },

}
</script>
